<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>饮品分类管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="layui-elem-field layuimini-search">
            <legend>搜索</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="#">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">分类名称</label>
                            <div class="layui-input-inline" style="width: 200px">
                                <input type="text" name="name" placeholder="输入名称搜索" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <a class="layui-btn" lay-submit="" lay-filter="data-search-btn">
                                <i class="layui-icon layui-icon-search"></i>搜索
                            </a>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">
                <i class="layui-icon layui-icon-edit"></i>编辑
            </a>
            <a class="layui-btn layui-btn-xs data-count-delete" lay-event="delete">
                <i class="layui-icon layui-icon-delete"></i>删除
            </a>
        </script>

    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        /**
         * 表格渲染
         */
        let tableIns = table.render({
            elem: '#currentTableId',
            // TODO: 调用查看所有分类的API接口
            url: '/category/',
            toolbar: '#toolbarDemo',
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    // TODO: 后期需要调整API返回值
                    "code": 1,
                    "msg": "",
                    "count": 5, //解析数据长度
                    "data": res //解析数据列表
                };
            },
            response: {
                statusName: 'code',
                statusCode: 1
            },
            cols: [
                [
                    {field: 'id', width: 100, title: '分类编号'},
                    {field: 'name', width: 300, title: '分类名称'},
                    {title: '操作', minWidth: 150, templet: '#currentTableBar', fixed: "right", align: "center"}
                ]
            ],
            limit: 10,
            page: true,
        });

        /**
         * 监听搜索操作
         */
        form.on('submit(data-search-btn)', function (data) {
            // 执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    type: data.field.type
                }
            }, 'data');
            $('#selTree-dept').val('');
            return false;
        });

        /**
         * 表头toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') { // 监听添加操作
                let index = layer.open({
                    title: '添加分类',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['60%', '50%'],
                    content: '/category/addform',
                    end: function() { // 子页关闭后调用
                        tableIns.reload();
                    },
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        /**
         * 数据行toolbar监听事件
         */
        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            if (obj.event === 'edit') {
                let index = layer.open({
                    title: '修改分类',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['60%', '50%'],
                    content: '/category/updateform?id=' + data.id,
                    end: function () { // 子页关闭后调用
                        tableIns.reload();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除该分类吗？', function (index) {
                    layer.close(index);

                    // TODO: 调用删除分类的API接口
                    $.ajax({
                        url: '/category/' + data.id,
                        method: 'delete',
                        success: (response) => {
                            tableIns.reload();
                        }
                    });
                });
            }
        });
    });
</script>
</body>
</html>